﻿namespace FFF.Test.Application.AppServices.Demo
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Application.Services.Dto;
    using Abp.Authorization;
    using Abp.UI;
    using Abp.Web.Models;
    using Abp.Domain.Uow;
    using Abp.Dependency;
    using Abp.Runtime.Caching;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    using Abp.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.Application.AppServices;
    using FFF.Test.Application.DomainServices;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.AppServices.PublicService.Sessions;
    using FFF.Test.Application.Framework.Dto;
    using Ydm.Helper;
    using Ydm.Extensions;
    using FFF.Test.Application.PublicService.AppService.TreeGrid;
    using FFF.Test.Application.AppServices.PublicService.Grid;
    using FFF.Test.Application.DomainServices.Demo;
    using FFF.Test.Application.Repositories.Demo;
    using FFF.Test.Entity.Demo;
    using FFF.Test.Entity.Demo.Dto;
    using FFF.Test.Entity.Demo.View;

    /// <summary>
    /// 学院表(树)应用层服务
    /// </summary>
    [YdmApiVersion(Version = "Demo")]
    public class DemoSchoolTeAppService : YdmAbpCurdAppServiceBase<DemoSchoolTeEntity, Guid, DemoSchoolTeCreateInputDto, DemoSchoolTeUpdateInputDto, DemoSchoolTeDto, ViewDemoSchoolTe, ViewDemoSchoolTe>, IDemoSchoolTeAppService
    {
        private readonly IDemoSchoolTeDomainService _demoSchoolTeDomainService;

        private readonly IDemoSchoolTeRepository _demoSchoolTeRepository;
        
        private readonly ISessionAppService _sessionAppservice;

        private readonly ICacheManager _cacheManager;

        private readonly IIocResolver _iocResolver;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="DemoSchoolTeAppService"/> class.
        /// 构造方法
        /// </summary>
        /// <param name="demoSchoolTeRepository">学院表(树)仓储</param>
        /// <param name="demoSchoolTeDomainService">学院表(树)领域层</param>
        /// <param name="sessionAppservice">session</param>
        /// <param name="cacheManager">缓存服务</param>
        /// <param name="iocResolver">依赖注入容器</param>
        public DemoSchoolTeAppService(
            IDemoSchoolTeRepository demoSchoolTeRepository,
            IDemoSchoolTeDomainService demoSchoolTeDomainService,
            ISessionAppService sessionAppservice,
            ICacheManager cacheManager,
            IIocResolver iocResolver
        )
        {
            this._demoSchoolTeRepository = demoSchoolTeRepository;
            this._demoSchoolTeDomainService = demoSchoolTeDomainService;
            this._sessionAppservice = sessionAppservice;
            this._cacheManager = cacheManager;
            this._iocResolver = iocResolver;
        }
        
        /// <summary>
        ///  获取树形列表
        /// </summary>
        /// <param name="inDto">树形输入dto</param>
        /// <returns>获取树形列表输出dto</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "00")]
        public async Task<List<TreeGridOutDto>> GetTreeGird(TreeGridInDto inDto)
        {
            var dto = await this._demoSchoolTeRepository.GetTreeGirdAsync(inDto);
            return dto;
        }

        /// <summary>
        ///  获取所有的父级的id 以,分割
        /// </summary>
        /// <param name="id">父级id</param>
        /// <returns>父级的id以,分割</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "00")]
        public async Task<string> GetFatherIds(Guid id)
        {
            var dto = await this._demoSchoolTeRepository.GetFatherIdsAsync(id);
            return dto;
        }
        #region 框架方法
        
        /// <summary>
        /// 导入[学院表(树)]表格数据
        /// </summary>
        /// <param name="inDto">获取导出表格数据输入dto</param>
        /// <returns>dto对象</returns>
        [HttpPost]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "01")]
        public override async Task<ImportOutDto> ImportExcel(ImportInDto inDto)
        {
            var outDto = await this._demoSchoolTeRepository.ImportExcelAsync(inDto);
            return outDto;
        }

        /// <summary>
        /// 导出[学院表(树)]表格数据
        /// </summary>
        /// <param name="inDto">获取导出表格数据输入dto</param>
        /// <returns>dto对象</returns>
        [HttpGet]
        [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "92")]
        public override async Task<FileContentResult> GetGridExcel(GetGridListInDto inDto)
        {
           var fileResult = await this._demoSchoolTeRepository.GetGridExcelAsync(inDto);
           return fileResult;
        }

        /// <summary>
        /// 获取  [学院表(树)] 表格数据
        /// </summary>
        /// <param name="inDto">表格数据输入的Dto</param>
        /// <returns>分页信息</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "00")]
        public override async Task<GridResult> GetGridDto(GetGridListInDto inDto)
        {
           var gridResult = await this._demoSchoolTeRepository.GetGridDtoAsync(inDto);
           return gridResult;
        }

        /// <summary>
        /// 获取单个  [学院表(树)] 视图的dto
        /// </summary>
        /// <param name="idDto">idDto集合</param>
        /// <returns>单个对象的dto</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "00")]
        public override async Task<ViewDemoSchoolTe> GetViewDto(PrimaryDto idDto)
        {
           var id = (Guid)idDto.Id.CastType(typeof(Guid));
           var dto = await this._demoSchoolTeRepository.GetViewDtoAsync(id);
           return dto;
        }
        
        ///// <summary>   
        /////  获取视图分页信息obj 通过select("new(CompanyName as Name, Phone)") 这种方式获取
        ///// </summary>
        ///// <param name="pagedInputDto">分页输入的Dto</param>
        ///// <returns>分页对象</returns>
        //[HttpPost]
        //[Authorize(Roles = "admin")]
        //[UnitOfWork(isTransactional: false)]
        //public override async Task<MyPagedResult<object>> GetViewPageSelect(PagedInputDto pagedInputDto)
        //{
            // var pagedResult = await this._demoSchoolTeRepository.GetViewPageSelectAsync(pagedInputDto);
            // return pagedResult;
        //}

        ///// <summary>
        ///// 获取  [学院表(树)] 视图分页的信息
        ///// </summary>
        ///// <param name="pagedInputDto">分页输入的Dto</param>
        ///// <returns>分页信息</returns>
        //[HttpPost]
        //[Authorize(Roles = "admin")]
        //[UnitOfWork(isTransactional: false)]
        //[YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "00")]
        //public override async Task<MyPagedResult<ViewDemoSchoolTe>> GetViewPage(PagedInputDto pagedInputDto)
        //{
           //var pagedResult = await this._demoSchoolTeRepository.GetViewPageAsync(pagedInputDto);
           //return pagedResult;
        //}

        /// <summary>
        /// 创建  [学院表(树)]
        /// </summary>
        /// <param name="input">创建输入实体</param>
        /// <returns>输出dto</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "01")]
        public override  async Task<DemoSchoolTeDto> CreateByDto(DemoSchoolTeCreateInputDto input)
        {
           var dto = await this._demoSchoolTeRepository.CreateByDtoAsync(input);
           return dto;
        }

        /// <summary>
        /// 更新  [学院表(树)]
        /// </summary>
        /// <param name="input">更新的实体的对象</param>
        /// <returns>更新后的对象</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "02")]
        public override async Task<DemoSchoolTeDto> UpdateByDto(DemoSchoolTeUpdateInputDto input)
        {
           var dto = await this._demoSchoolTeRepository.UpdateByDtoAsync(input);
           return dto;
        }
        
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="idDto">idDto集合</param>
        /// <returns>空</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "03")]
        public override async Task DeleteByDto(PrimaryDto idDto)
        {
           var id = (Guid)idDto.Id.CastType(typeof(Guid));
           await this._demoSchoolTeRepository.DeleteAsync(id);
        }
        
        /// <summary>
        /// 批量删除  [学院表(树)] 传递包含id的dto
        /// </summary>
        /// <param name="idDto">idDto集合</param>
        /// <returns> task 空值</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "Demo_SchoolTe", ActionCode = "03")]
        public override async Task BatchDelete(PrimaryDto idDto)
        {
           string strId = idDto.Id;
           IEnumerable<string> ids = strId.Split(",").Select(x => x.Replace("'", string.Empty));
           List<Guid> newIds = new List<Guid>();
           foreach (var id in ids)
           {
               await this._demoSchoolTeRepository.DeleteAsync((Guid)id.CastType(typeof(Guid)));
           }
        }
        #endregion
    }
}
